home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / Waste TCL r2 / WASTEEdit ƒ / CEditApp.cp next >
Encoding:
Text File  |  1994-11-30  |  6.8 KB  |  302 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     CEditApp.c
  3.     
  4.     Application methods for a tiny editor.
  5.         
  6.     Copyright © 1989 Symantec Corporation. All rights reserved.
  7.  
  8.  ******************************************************************************/
  9.  
  10. #include "Commands.h"
  11. #include "CBartender.h"
  12. #include "CEditApp.h"
  13. #include "CEditDoc.h"
  14. #include "Global.h"
  15. #include "WASTE.h"
  16. #ifndef __SCRIPT__
  17. #include <Script.h>
  18. #endif
  19. #include <TextServices.h>
  20. #include <GestaltEqu.h>
  21.  
  22. #include "CTSMSwitchboard.h"
  23.  
  24. static Boolean    TSMAvailable(void);
  25.  
  26. short gUsingTSM = false;
  27.  
  28. extern    OSType        gSignature;
  29. extern    CBartender *gBartender;
  30.  
  31. #define        kExtraMasters        10
  32. #define        kRainyDayFund        45000
  33. #define        kCriticalBalance    40000
  34. #define        kToolboxBalance        20000
  35.  
  36.  
  37. /***
  38.  * IEditApp
  39.  *
  40.  *    Initialize the application. Your initialization method should
  41.  *    at least call the inherited method. If your application class
  42.  *    defines its own instance variables or global variables, this
  43.  *    is a good place to initialize them.
  44.  *
  45.  ***/
  46.  
  47. void CEditApp::IEditApp(void)
  48.  
  49. {
  50.     gUsingTSM = TSMAvailable();
  51.     if (gUsingTSM) gUsingTSM = (InitTSMAwareApplication()==noErr); // can we init?
  52.  
  53.     CApplication::IApplication( kExtraMasters, kRainyDayFund,
  54.                         kCriticalBalance, kToolboxBalance);
  55.  
  56. }
  57.  
  58.  
  59.  
  60. /***
  61.  * SetUpFileParameters
  62.  *
  63.  *    In this routine, you specify the kinds of files your
  64.  *    application opens.
  65.  *
  66.  *
  67.  ***/
  68.  
  69. void CEditApp::SetUpFileParameters(void)
  70.  
  71. {
  72.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  73.  
  74.         /**
  75.          **    sfNumTypes is the number of file types
  76.          **    your application knows about.
  77.          **    sfFileTypes[] is an array of file types.
  78.          **    You can define up to 4 file types in
  79.          **    sfFileTypes[].
  80.          **
  81.          **/
  82.  
  83.     sfNumTypes = 1;
  84.     sfFileTypes[0] = 'TEXT';
  85.  
  86.         /**
  87.          **    Although it's not an instance variable,
  88.          **    this method is a good place to set the
  89.          **    gSignature global variable. Set this global
  90.          **    to your application's signature. You'll use it
  91.          **    to create a file (see CFile::CreateNew()).
  92.          **
  93.          **/
  94.  
  95.     gSignature = '???\?';
  96. }
  97.  
  98.  
  99. /***
  100.  * SetUpMenus
  101.  *
  102.  *    In this method, you add special menu items and set the
  103.  *    menu item dimming and checking options for your menus.
  104.  *    The most common special menu items are the names of the
  105.  *    fonts. For this tiny editor, you also want to set up the
  106.  *    dimming and checking options so only the current font
  107.  *    and size are checked.
  108.  *
  109.  ***/
  110.  
  111. void CEditApp::SetUpMenus(void)
  112.  
  113. {
  114.         /**
  115.          ** Let the default method read the menus from
  116.          **    the MBAR 1 resource.
  117.          **
  118.          **/
  119.  
  120.     inherited::SetUpMenus();
  121.  
  122.         /**
  123.          ** Add the fonts in the  system to the
  124.          **    Font menu. Remember, MENUfont is one
  125.          **    of the reserved font numbers.
  126.          **
  127.          **/
  128.  
  129.     AddResMenu(GetMHandle(MENUfont), 'FONT');
  130.  
  131.         /**
  132.          **    The UpdateMenus() method sets up the dimming
  133.          **    for menu items. By default, the bartender dims
  134.          **    all the menus, and each bureaucrat is reponsible
  135.          **    for turning on the items that correspond to the commands
  136.          **    it can handle.
  137.          **
  138.          **    Set up the options here. The edit pane's UpdateMenus()
  139.          **    method takes care of doing the work.
  140.          **
  141.          **    For Font and Size menus, you want all the items to
  142.          **    be enabled all the time. In other words, you don't
  143.          **    want the bartender to ever dim any of the items
  144.          **    in these two menus.
  145.          **
  146.          **/
  147.  
  148.     gBartender->SetDimOption(MENUfont, dimNONE);
  149.     gBartender->SetDimOption(MENUsize, dimNONE);
  150.     gBartender->SetDimOption(MENUstyle, dimNONE);
  151.  
  152.         /**
  153.          **    For Font and Size menus, one of the items
  154.          **    is always checked. Setting the unchecking option
  155.          **    to TRUE lets the bartender know that it should
  156.          **    uncheck all the menu items because an UpdateMenus()
  157.          **    method will check the right items.
  158.          **    For the Style menu, uncheck all the items and
  159.          **    let the edit pane's UpdateMenus() method check the
  160.          **    appropriate ones.
  161.          **
  162.          **/
  163.  
  164.     gBartender->SetUnchecking(MENUfont, TRUE);
  165.     gBartender->SetUnchecking(MENUsize, TRUE);
  166.     gBartender->SetUnchecking(MENUstyle, TRUE);
  167. }
  168.  
  169.  
  170.  
  171. /***
  172.  * CreateDocument
  173.  *
  174.  *    The user chose New from the File menu.
  175.  *    In this method, you need to create a document and send it
  176.  *    a NewFile() message.
  177.  *
  178.  ***/
  179.  
  180. void CEditApp::CreateDocument()
  181.  
  182. {
  183.     CEditDoc    *theDocument = NULL;
  184.     
  185.     // In the event that creating the document fails,
  186.     // we setup an exception handler here. If any
  187.     // of the methods called within the scope of this
  188.     // TRY block fail, an exception will be raised and
  189.     // control will be transferred to the CATCH block.
  190.     // Here, the CATCH block takes care of disposing
  191.     // of the partially created document.
  192.     
  193.     TRY
  194.     {
  195.         theDocument = new(CEditDoc);
  196.             
  197.             /**
  198.              **    Send your document an initialization
  199.              **    message. The first argument is the
  200.              **    supervisor (the application). The second
  201.              **    argument is TRUE if the document is printable.
  202.              **
  203.              **/
  204.         
  205.         theDocument->IEditDoc(this, TRUE);
  206.     
  207.             /**
  208.              **    Send the document a NewFile() message.
  209.              **    The document will open a window, and
  210.              **    set up the heart of the application.
  211.              **
  212.              **/
  213.         theDocument->NewFile();
  214.     }
  215.     CATCH
  216.     {
  217.         ForgetObject( theDocument);
  218.     }
  219.     ENDTRY;
  220. }
  221.  
  222. /***
  223.  * OpenDocument
  224.  *
  225.  *    The user chose Open… from the File menu.
  226.  *    In this method you need to create a document
  227.  *    and send it an OpenFile() message.
  228.  *
  229.  *    The macSFReply is a good SFReply record that contains
  230.  *    the name and vRefNum of the file the user chose to
  231.  *    open.
  232.  *
  233.  ***/
  234.  
  235. void CEditApp::OpenDocument(SFReply *macSFReply)
  236.  
  237. {
  238.     CEditDoc    *theDocument = NULL;
  239.  
  240.     // In the event that opening the document fails,
  241.     // we setup an exception handler here. If any
  242.     // of the methods called within the scope of this
  243.     // TRY block fail, an exception will be raised and
  244.     // control will be transferred to the CATCH block.
  245.     // Here, the CATCH block takes care of disposing
  246.     // of the partially opened document.
  247.  
  248.     TRY
  249.     {    
  250.         theDocument = new(CEditDoc);
  251.             
  252.             /**
  253.              **    Send your document an initialization
  254.              **    message. The first argument is the
  255.              **    supervisor (the application). The second
  256.              **    argument is TRUE if the document is printable.
  257.              **
  258.              **/
  259.         
  260.         theDocument->IEditDoc(this, TRUE);
  261.     
  262.             /**
  263.              **    Send the document an OpenFile() message.
  264.              **    The document will open a window, open
  265.              **    the file specified in the macSFReply record,
  266.              **    and display it in its window.
  267.              **
  268.              **/
  269.         theDocument->OpenFile(macSFReply);
  270.     }
  271.     CATCH
  272.     {
  273.         ForgetObject( theDocument);
  274.     }
  275.     ENDTRY;
  276. }
  277.  
  278. /*** Dispose added for WASTE support ***/
  279. void CEditApp::Dispose(void)
  280. {
  281.         if (gUsingTSM) CloseTSMAwareApplication();
  282. }
  283.  
  284. /*** make TSMSwitchboard ***/
  285. void CEditApp::MakeSwitchboard( void)
  286. {
  287.     if (gSystem.hasAppleEvents && gUsingTSM)
  288.     {
  289.         FailOSErr(WEInstallTSMHandlers());
  290.     }
  291.     itsSwitchboard = (CSwitchboard *)new CTSMSwitchboard();
  292.     itsSwitchboard->InitAppleEvents(); // needed for TCL 2.0.3
  293. }
  294.  
  295.  
  296. static Boolean TSMAvailable(void)
  297. {
  298.     long    response;
  299.     
  300.     return (Gestalt(gestaltTSMgrVersion, &response)==noErr);
  301. }
  302.